package simulation;

import java.util.ArrayList;
import java.util.Collection;

import agents.Agent;
import agents.resource.ResourceAgent;
import agents.task.TaskAgent;

public class AgentManager {
	/* ***** resource agents **** */
	
	public ResourceAgent getResourceAgentOf(String infrastructureId) {
		for(ResourceAgent resAgent : resourceAgents) {
			if(resAgent.getTrafficInfrastructure().getId().equals(infrastructureId)) return resAgent;
		}
		return null;
	}
	
	private Collection<ResourceAgent> resourceAgents = new ArrayList<ResourceAgent>();
	
	public void addResourceAgent(ResourceAgent agent) {
		resourceAgents.add(agent);
	}
	public void addAllResourceAgents(Collection<ResourceAgent> agents){
		resourceAgents.addAll(agents);
	}
	
	public void removeResourceAgent(ResourceAgent agent) {
		resourceAgents.remove(agent);
	}
	
	public Collection<ResourceAgent> getAllResourceAgents() {
		return resourceAgents;
	}
	
	/* ***** task agents ***** */
	public TaskAgent getTaskAgentOfCar(String label) {
		for(TaskAgent taskAgent : getTaskAgents()) {
			if(taskAgent.getCar().getId().equals(label)) return taskAgent;
		}
		return null;
	}
	
	private Collection<TaskAgent> taskAgents = new ArrayList<TaskAgent>();
	
	public void addTaskAgent(TaskAgent agent) {
		getTaskAgents().add(agent);
	}
	public void addAllTaskAgents(Collection<TaskAgent> agents){
		getTaskAgents().addAll(agents);
	}
	public void removeTaskAgent(TaskAgent agent) {
		getTaskAgents().remove(agent);
	}
	
	public Collection<TaskAgent> getAllTaskAgents() {
		return new ArrayList<TaskAgent>(getTaskAgents());
	}
	
	
	public Collection<Agent> getAllAgents() {
		Collection<Agent> allAgents = new ArrayList<Agent>(getAllTaskAgents());
		allAgents.addAll(getAllResourceAgents());
		return allAgents;
	}
	
	private Collection<TaskAgent> getTaskAgents() {
		return taskAgents;
	}
	
	
	public void addAllAgents(Collection<Agent> newAgents) {
		for(Agent a : newAgents) {
			if(a instanceof TaskAgent) addTaskAgent((TaskAgent) a);
			else addResourceAgent((ResourceAgent)a);
		}
	}
	
	public void removeAgents(Collection<Agent> agentsToRemove) {
		for(Agent a : agentsToRemove) {
			if(a instanceof TaskAgent) removeTaskAgent((TaskAgent) a);
			else removeResourceAgent((ResourceAgent) a);
		}
	}
	
}
